﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SignalR;

namespace Quantum.SignalR
{
    public class ServerStartup
    {
        public virtual void ConfigureServices(IServiceCollection services)
        {
            services.AddSockets();
            services.AddSignalR();
            
            services.AddCors(o =>
            {
                o.AddPolicy("Everything", p =>
                {
                    p.AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowAnyOrigin();
                });
            });
        }

        public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("Everything");

            app.UseSignalR(ConfigureRoutes);
        }

        protected virtual void ConfigureRoutes(HubRouteBuilder routes)
        {
            routes.MapHub<EchoReplier>("default");
            routes.MapHub<HelloBroadcaster>("hello");
        }
    }
}
